
package ejb;

import java.util.*;

import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

import ejb.CustomerFacadeRemote;
import ejb.CustomerFacade;

import jpa.CategoryJPA;
import jpa.CustomerJPA;
import jpa.ShippingAddressJPA;

/**
 * EJB Session Bean Class of example "Practical Case Study JEE"
 */
@Stateless
public class CustomerFacadeBean implements CustomerFacade, CustomerFacadeRemote {

	//Persistence Unit Context
	@PersistenceContext(unitName="PracticalCase") 
	private EntityManager entman;
	
	
	/**
	 * Method to log in
	 */
	
	public CustomerJPA login(String email, String pwd) {
		CustomerJPA customer = new CustomerJPA();
		try{
			@SuppressWarnings("unchecked")
			Collection<CustomerJPA> customers = entman.createQuery("FROM CustomerJPA c WHERE c.email = '"+email+"' AND c.password = '"+pwd+"'").getResultList();
			if (!customers.isEmpty() || customers.size()==1)
			{
				Iterator<CustomerJPA> iter = customers.iterator();
				customer = (CustomerJPA) iter.next();
			}
			
		}catch (PersistenceException e) {
			System.out.println(e);
		}
		return customer;
	}
	
	/**
	 * Method to find a customer using id
	 */
	@Override
	public CustomerJPA showCustomer(String id) {
		CustomerJPA customer = new CustomerJPA();
		try
		{
			@SuppressWarnings("unchecked")
			Collection<CustomerJPA> customers = entman.createQuery("FROM CustomerJPA c WHERE c.nif = '"+id+"'").getResultList();
			if (!customers.isEmpty() || customers.size()==1)
			{
				Iterator<CustomerJPA> iter = customers.iterator();
				customer = (CustomerJPA) iter.next();				
			}
		}catch (PersistenceException e) {
			System.out.println(e);
		} 
	    return customer;
	}

	/**
	 * Method to log out
	 */
	public CustomerJPA logout() {
		return new CustomerJPA();

	}

	/**
	 * Method that returns instance of the class customer
	 */
	public CustomerJPA register(CustomerJPA customer) {
		entman.persist(customer);
		return customer;
	}

	@Override
	public java.util.Collection<CustomerJPA> findAllCustomers() {
		@SuppressWarnings("unchecked")
		Collection<CustomerJPA> allCustomers = entman.createQuery("from CustomerJPA").getResultList();
		return allCustomers;
	}

	@Override
	public CustomerJPA modifyData(CustomerJPA customer) {
		try{
			entman.merge(customer);
		}catch (PersistenceException e) {
			System.out.println(e);
			return null;
		}
		return customer;
	}

	@Override
	public boolean changePassword(String email, String new_p, String old_p) {
		CustomerJPA customer = new CustomerJPA();
		try{
			@SuppressWarnings("unchecked")
			Collection<CustomerJPA> customers = entman.createQuery("FROM CustomerJPA c WHERE c.email = '"+email+"' AND c.password = '"+old_p+"'").getResultList();
			if (!customers.isEmpty() || customers.size()==1)
			{
				Iterator<CustomerJPA> iter = customers.iterator();
				customer = (CustomerJPA) iter.next();
				customer.setPassword(new_p);
				this.modifyData(customer);
				return true;
			}
			
		}catch (PersistenceException e) {
			System.out.println(e);
		}
		return false;
	}

	@Override
	public void addShippingAddress(ShippingAddressJPA shipping) {
		entman.persist(shipping);
	}

	@Override
	public void updateShippingAddress(ShippingAddressJPA shipping) {
		entman.merge(shipping);
	}
	
	@Override
	public int counter(){
		return entman.createQuery("FROM ShippingAddressJPA").getResultList().size()+1;
	}

}
